//
// Created by v on 19-6-11.
//

#include "config/CMConfigGeneral.h"
#include "config/CMConfigSimple.h"
#include "utils/cm_logprint.h"

CMLogPrint logPrint;

void test_config_simple_init(const char *filename)
{
    CMConfigSimple cs(filename);
    cs.setString("testint", "128");
    cs.setString("teststring", "hello body");
    cs.setString("testdouble", "9.89");
    cs.setString("testbool", "true");
    cs.writeBack();
}

void test_config_simple(const char *filename)
{
    CMConfigSimple cs(filename);
    int            i = cs.getInt("testint");
    std::string    result;
    cs.getString("teststring", result);
    bool b = false;
    cs.getBool("testbool", b);
    double x = 0.0;
    cs.getDouble("testdouble", x);
    int ret = cs.getInt("noexist");
    cminfo("read result: i = %d, b = %d, x = %f, result = %s, ret = %d\n", i, b,
           x, result.c_str(), ret);
}

void test_config_general(const char *filename)
{
    CMConfigCpp *c = new CMConfigGeneral(filename);
    std::string  v;
    c->toString(v);
    cminfo("output file [%s]\n", v.c_str());
    Json::Value root;
    c->getRoot(root);
    root["x"] = "testresult";
    c->parse(root.toStyledString());
    c->writeFile();
}

int main(int argc, char **args)
{
    char *filename = args[1];
    if (!filename)
    {
        return -1;
    }
    CM_LOG_PRINT_INIT("cm_config");

    cminfo("parse filename = [%s]\n", filename);

    //    test_config_general(filename);
    test_config_simple_init(filename);
    test_config_simple(filename);
    getchar();

    return 0;
}
